Chapter 8

Code example 8-1
Private Sub FontSize_NotInList(NewData As String, _
Response As Integer)
Dim ctlComboBox As ComboBox
'Assign the ctlComboBox as the FontSize property.
Set ctlComboBox = Me!FontSize
'Prompt user to verify the added value.
If MsgBox("This value is not on the list. Add it?", _
    vbOKCancel) = vbOK Then
    'Set the Response argument to show item is added.
    Response = acDataErrAdded
    'Add the entered value to the row source with a semicolon.
    ctlComboBox.RowSource = ctlComboBox.RowSource & ";" & NewData
Else
    'User chooses Cancel, suppress error message and undo change.
    Response = acDataErrContinue
    ctlComboBox.Undo
End If
End Sub
Code example 8-2
Private Sub Command6_Click()
Dim strCode As String
strCode = " [Activity Code] = " _
    & InputBox("Enter the activity code", "Filter records")
Me.Filter = strCode
Me.FilterOn = True
End Sub
Code example 8-3
Private Sub Field2_OnLostFocus()
Dim strMsg As String, strKey as String
Dim ctlField As Control
strMsg = "You must enter a Field2 value for an ABC transaction."
strKey = [Field1]               'Set the value of the key to the Field1 value.
ctlField = Me!Field1
If [Field1] = "ABC" And  [Field2] = "" Then
    MsgBox = strMsg             'Display the error message
    DoCmd.GoToControl ctlField  'Move back to Field1
    DoCmd.FindRecord strKey     'Find the record
    Me.Field2.SetFocus          'Return focus to Field2
End If
End Sub
Code example 8-4
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(LastName) Then
    MsgBox "LastName is required"
    DoCmd.GoToRecord , , acLast 'Move back to previous record
    LastName.SetFocus     'Move cursor back toLastName field.
End If
End Sub
Code example 8-5
Private Sub Form_Open(Cancel As Integer)
'Removes both scrollbars and adds the record selector.
'Adds a caption and a ScreenTip to the command button.
'Adds a status bar message for the Activity Code control.
Me.RecordSelectors = True
Me.ScrollBars = 0
Me.Caption = "DAR Activity Codes"
Me!Command6.Caption = "&Find Code"
Me!Command6.ControlTipText = "Click to find activity code"
Me![Activity Code].StatusBarText = "Enter Acty code"
End Sub
Code example 8-6
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
'Set the font weight constants
Const conHeavy = 900
Const conNormal = 400
'Test the DueDate value, if earlier than today, make changes.
If DueDate < Date Then
    DueDate.BackColor = vbYellow
    DueDate.ForeColor = vbBlack
    DueDate.FontWeight = conHeavy
Else
    DueDate.BackColor = vbBlack
    DueDate.ForeColor = vbWhite
    DueDate.FontWeight = conNormal
End If
End Sub
Code example 8-7
Public Function FindACDescription()
'This function brings up the description of any
'activity code that the user enters into the input box.

Dim lngActivityCode As Long

strActivityCode = "[Activity Code] = " _
& InputBox("Enter an activity code")
MsgBox DLookup("Description", "Activity Codes", strActivityCode)

End Function
Code example 8-8
Private Sub ArchiveDARsBetweenDates()

'This procedure appends a set of records from the DARs table
'where the dates are between the time range input by the user.
Dim dbs As Database
Dim rst As Recordset
Dim strSQL As String
Dim dteSDate As Date
Dim dteEDate As Date
'Turn off the warning about deleting records.
DoCmd.SetWarnings False

dteSDate = InputBox("Enter Start Date")
dteEDate = InputBox("Enter End Date")

strSQL = "INSERT INTO DARArchive SELECT DARs.* " _
& "FROM DARs WHERE (((DARs.DARDate) Between " _
& "#" & dteSDate & "# And #" & dteEDate & "#));"

DoCmd.RunSQL strSQL
'Restore the warning window.
DoCmd.SetWarnings True
End Sub
Access Power Programming with VBA, 8/23/2003, Web code examples
Virginia Andersen


